package main

import (
	"bufio"
	"fmt"
	"os"
)

func countlines(f *os.File, counters map[string]int) {
	// 读取文件
	input := bufio.NewScanner(f)
	// 扫描每一行
	for input.Scan() {
		counters[input.Text()]++
		if counters[input.Text()] > 1 {
			fmt.Printf("[+] dup lines! %s\t in %s\n", input.Text(), f.Name())
		}
	}
}

func main() {
	counters := make(map[string]int)
	files := os.Args[1:]
	if len(files) == 0 {
		countlines(os.Stdin, counters)
	} else {
		for _, arg := range files {
			f, err := os.Open(arg)
			if err != nil {
				fmt.Fprintf(os.Stderr, "dup2: %v\n", err)
				continue
			}
			// 获取每一个文件，分别执行countlines()
			countlines(f, counters)
			f.Close()
		}
	}

	for line, n := range counters {
		if n > 1 {
			fmt.Printf("%d\t%s\n", n, line)
		}
	}
}
